package com.autonavi.yunda.yunji.core.engine.configuration;

import com.autonavi.yunda.yunji.common.thread.ThreadPoolMonitor;
import com.autonavi.yunda.yunji.common.thread.TraceableThreadPoolTaskExecutor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author cizhong.wcs
 * @date 2022/6/21 11:06
 */
@Configuration
public class ExecutorConfiguration {
    @Bean
    public TraceableThreadPoolTaskExecutor engineWorkerExecutor() {
        TraceableThreadPoolTaskExecutor engineWorkerExecutor = new TraceableThreadPoolTaskExecutor();
        engineWorkerExecutor.setCorePoolSize(8);
        engineWorkerExecutor.setMaxPoolSize(200);
        engineWorkerExecutor.setQueueCapacity(0);
        engineWorkerExecutor.setThreadNamePrefix("engine-worker-thread ");
        engineWorkerExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        engineWorkerExecutor.initialize();
        return engineWorkerExecutor;
    }

    @Bean
    public TraceableThreadPoolTaskExecutor tairAsyncExecutor() {
        TraceableThreadPoolTaskExecutor tairAsyncExecutor = new TraceableThreadPoolTaskExecutor();
        tairAsyncExecutor.setCorePoolSize(8);
        tairAsyncExecutor.setMaxPoolSize(32);
        tairAsyncExecutor.setQueueCapacity(32);
        tairAsyncExecutor.setThreadNamePrefix("tair-pool-thread ");
        tairAsyncExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        tairAsyncExecutor.initialize();
        return tairAsyncExecutor;
    }

    @Bean
    public ThreadPoolMonitor threadPoolMonitor(@Autowired List<TraceableThreadPoolTaskExecutor> executorList) {
        return new ThreadPoolMonitor(executorList);
    }
}
